#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("文本编辑器");
    QObject::connect(ui->actionNew,SIGNAL(triggered(bool)),this,SLOT(newFile()));
    QObject::connect(ui->actionOpen,SIGNAL(triggered(bool)),this,SLOT(openFile()));
    QObject::connect(ui->actionSave,SIGNAL(triggered(bool)),this,SLOT(saveFile()));
    QObject::connect(ui->actionSave_As,SIGNAL(triggered(bool)),this,SLOT(saveASFile()));
    QObject::connect(ui->actionPrint,SIGNAL(triggered(bool)),this,SLOT(printFile()));

    QObject::connect(ui->actionUndo,SIGNAL(triggered(bool)),ui->textEdit,SLOT(undo()));
    QObject::connect(ui->actionRedo,SIGNAL(triggered(bool)),ui->textEdit,SLOT(redo()));
    QObject::connect(ui->actionCopy,SIGNAL(triggered(bool)),ui->textEdit,SLOT(copy()));
    QObject::connect(ui->actionCut,SIGNAL(triggered(bool)),ui->textEdit,SLOT(cut()));
    QObject::connect(ui->actionPaste,SIGNAL(triggered(bool)),ui->textEdit,SLOT(paste()));
    QObject::connect(ui->actionSelectAll,SIGNAL(triggered(bool)),ui->textEdit,SLOT(selectAll()));

    QObject::connect(ui->actionFont,SIGNAL(triggered(bool)),this,SLOT(fontFile()));
    QObject::connect(ui->actionColor,SIGNAL(triggered(bool)),this,SLOT(colorFile()));
    QObject::connect(ui->actionDate,SIGNAL(triggered(bool)),this,SLOT(dateFile()));

    this->connect(ui->actionExit,SIGNAL(triggered(bool)),this,SLOT(close()));

    QObject::connect(ui->actionAboutQt,SIGNAL(triggered(bool)),qApp,SLOT(aboutQt()));
    QObject::connect(ui->actionAboutWeb,SIGNAL(triggered(bool)),this,SLOT(openWebSite()));
    QObject::connect(ui->actionAboutUs,SIGNAL(triggered(bool)),this,SLOT(aboutUSFunc()));


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::newFile(){
    if(ui->textEdit->document()->isModified()){
        qDebug()<<"文本已被修改";
        ui->textEdit->clear();
        this->setWindowTitle("Untitled-notepad");
    } else {
        qDebug()<<"文本未被修改";
    }
}

void MainWindow::openFile(){
    fileNameStr = QFileDialog::getOpenFileName(this, "打开文件", QDir::currentPath());
    if(fileNameStr.isEmpty()){
        QMessageBox::information(this, "ERROR", "打开文件错误");
    } else {
        QFile *file = new QFile(this);
        file->setFileName(fileNameStr);
        bool ok = file->open(QIODevice::ReadOnly);
        if(ok){
            QTextStream in(file);
            ui->textEdit->setText(in.readAll());
            this->setWindowTitle("正在编辑 " + fileNameStr);
            file->close();
            delete file;
        } else {
            QMessageBox::information(this, "ERROR", "打开文件错误");
        }
    }
}

void MainWindow::saveFile(){
    if(fileNameStr.isEmpty()){
        this->saveASFile();
    } else {
        QFile *file = new QFile(this);
        file->setFileName(fileNameStr);
        bool ok = file->open(QIODevice::WriteOnly);
        if(ok){
             QTextStream out(file);
             out<<ui->textEdit->toPlainText();
             file->close();
             delete file;
        } else {
            QMessageBox::information(this, "ERROR", "保存失败");
        }
    }
}

void MainWindow::saveASFile(){
    fileNameStr = QFileDialog::getSaveFileName(this, "文件另存为", QDir::currentPath());
    QFile *file = new QFile(this);
    file->setFileName(fileNameStr);
    bool ok = file->open(QIODevice::WriteOnly);
    if(ok){
        QTextStream out(file);
        out<<ui->textEdit->toPlainText();
        this->setWindowTitle("正在编辑 " + fileNameStr);
        file->close();
        delete file;
    } else {
        QMessageBox::information(this, "ERROR", "保存失败");
    }
}

void MainWindow::printFile(){
    QString printFilePath = QFileDialog::getSaveFileName(this, "导出PDF文件", "*.pdf");
    if(!printFilePath.isEmpty()){
        if(QFileInfo(printFilePath).suffix().isEmpty()){
            printFilePath.append(".pdf");
        }
        QPrinter printer;
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOutputFileName(printFilePath);
        ui->textEdit->print(&printer);
        QMessageBox::information(this, "提示", "导出成功");

    } else {
        QMessageBox::information(this, "ERROR", "导出错误");
    }


}

void MainWindow::fontFile(){
    bool ok;
    QFont font = QFontDialog::getFont(&ok, this);
    if(ok){
        ui->textEdit->setFont(font);
    } else {
        QMessageBox::information(this, "ERROR", "字体设置错误");
    }

}

void MainWindow::colorFile(){
    QColor color = QColorDialog::getColor(Qt::black, this);
    if(color.isValid()){
        ui->textEdit->setTextColor(color);
    } else {
        QMessageBox::information(this, "ERROR", "设置颜色错误");
    }
}

void MainWindow::dateFile(){
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString currentDateTimeStr = currentDateTime.toString("yyyy-MM-dd hh-mm-ss");
    ui->textEdit->append(currentDateTimeStr);
}

void MainWindow::openWebSite(){
    QDesktopServices::openUrl(QUrl("http://www.hello2018.cn/"));
}

void MainWindow::aboutUSFunc(){
    aboutus *dialog = new aboutus(this);
    dialog->show();
}

void MainWindow::closeEvent(QCloseEvent *event){
    if(ui->textEdit->document()->isModified()){
        QMessageBox msgBox;
        msgBox.setWindowTitle("关闭确认");
        msgBox.setText("你的文档已修改且尚未保存，请确认");
        msgBox.setInformativeText("请问需要保存吗？");
        msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Cancel);
        msgBox.setButtonText(QMessageBox::Save, "保存");
        msgBox.setButtonText(QMessageBox::Discard, "不保存");
        msgBox.setButtonText(QMessageBox::Cancel, "取消");
        int selectValue = msgBox.exec();
        if(selectValue == QMessageBox::Save){
            this->saveFile();
        } else if(selectValue == QMessageBox::Discard){
            event->accept();
        } else if(selectValue == QMessageBox::Cancel){
            event->ignore();
        }
    } else {
        event->accept();
    }
}
